<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem
                        type="remoteSelect"
                        :label="t('article.article.category')"
                        v-model="baTable.form.items!.cat_id"
                        prop="cat_id"
                        :placeholder="t('article.article.category')"
                        :input-attr="{
                            params: { isTree: true, search: [{ field: 'parent_id', val: '2', operator: 'eq' }] },
                            pk: 'cat_id',
                            field: 'name',
                            'remote-url': '/admin/category/index',
                        }"
                    />
                    <el-form-item prop="title" :label="t('article.article.title')">
                        <el-input
                            v-model="baTable.form.items!.title"
                            type="string"
                            :placeholder="t('Please input field', { field: t('article.article.title') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem prop="content"
                              :label="t('article.article.content')"
                              type="editor"
                              v-model="baTable.form.items!.content"
                              :input-attr="{
                            editorType: 'wang',
                        }"
                    />
                    <FormItem prop="thumb" :label="t('article.article.thumb')" type="image" v-model="baTable.form.items!.thumb" />
                    <el-form-item>
                        <div class="block-help">
                            建议尺寸：790 x 590
                        </div>
                    </el-form-item>
                    <el-form-item prop="seo_title" :label="t('article.article.seo title')">
                        <el-input
                            v-model="baTable.form.items!.seo_title"
                            type="string"
                            :placeholder="t('Please input field', { field: t('article.article.seo title') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="keywords" :label="t('article.article.keywords')">
                        <el-input
                                v-model="baTable.form.items!.keywords"
                                type="string"
                                :placeholder="t('Please input field', { field: t('article.article.keywords') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="motto" :label="t('article.article.description')">
                        <el-input
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            v-model="baTable.form.items!.description"
                            type="textarea"
                            :placeholder="t('Please input field', { field: t('article.article.description') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="sort" :label="t('article.article.sort')">
                      <el-input
                          v-model="baTable.form.items!.sort"
                          type="number"
                          :placeholder="t('Please input field', { field: t('article.article.sort') })"
                      ></el-input>
                    </el-form-item>
                    <FormItem label="发布时间" type="datetime" v-model="baTable.form.items!.create_time" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { buildValidatorData } from '/@/utils/validate'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    cat_id: [buildValidatorData({ name: 'required', message: t('Please select field', t('article.article.category')) })],
    title: [buildValidatorData({ name: 'required', title: t('article.article.title') })],
    // content: [buildValidatorData({ name: 'editorRequired', title: t('article.article.content') })],
    thumb: [buildValidatorData({ name: 'required', title: t('article.article.thumb') })],
    // status: [buildValidatorData({ name: 'required', title: t('article.article.status') })],
})
</script>
